﻿//-----------------------------------------------------------------
//
// @(#)$Id: RefundOrder.aspx.cs,v 1.0 2013/12/13 17:57:32 lichunhua Exp $
// @(#)$Author: lichunhua  $
// @(#)$Date: 2013/12/13 17:57:32 $
// @(#)$Description:
//
//
//
//                 All Rights Reserved.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Web.UI.WebControls;
using ebay.Dal;
using ebay.Dal.CenterDB_EbayTableAdapters;
using ebay.Dal.CenterDB_OrderTableAdapters;
using ebay.Dal.K3DataBaseTableAdapters;

public partial class UI_Orders_RefundOrder : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ddlStatusId.Items.Insert(0, new ListItem("《--请选择--》", " "));
        ddlStatusId.Items.Insert(1, new ListItem("未审核", "0"));
        ddlStatusId.Items.Insert(2, new ListItem("已审核", "1"));
        ddlStatusId.Items.Insert(3, new ListItem("作废", "2"));
        ddlStatusId.Items.Insert(4, new ListItem("已付款", "3"));
        ddlStatusId.Items.Insert(5, new ListItem("已二次退款", "4"));

        //退款类型
        T_RefundTypeTableAdapter rTypeAdapter = new T_RefundTypeTableAdapter();
        ddltkrefType.DataSource = rTypeAdapter.GetData();
        ddltkrefType.DataValueField = "Id";
        ddltkrefType.DataTextField = "RefundType";
        ddltkrefType.DataBind();
        //部门
        T_DepartMentTableAdapter dAdapter = new T_DepartMentTableAdapter();
        ddltkdepartment.DataSource = dAdapter.GetData();
        ddltkdepartment.DataValueField = "FitemId";
        ddltkdepartment.DataTextField = "FName";
        ddltkdepartment.DataBind();
        //投诉
        ddltkdisputeStatus.Items.Insert(0, new ListItem("未投诉", "0"));
        ddltkdisputeStatus.Items.Insert(0, new ListItem("已投诉", "1"));

        //帐号
        T_EbayAccountTableAdapter ebayAccountAdapter = new T_EbayAccountTableAdapter();
        ddlebayUserId.DataSource = ebayAccountAdapter.GetData();
        ddlebayUserId.DataTextField = "EbayUserId";
        ddlebayUserId.DataValueField = "EbayUserId";
        ddlebayUserId.DataBind();
        ddlebayUserId.Items.Insert(0, new ListItem("《--请选择--》", "Select"));
        
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        WebUtility.CheckRight();
        if (!IsPostBack)
        {
            dateFrom.Value = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            dateTo.Value = DateTime.Now.ToString("yyyy-MM-dd");
        }

        T_TotalRefundAmountTableAdapter TotalRefundAmount = new T_TotalRefundAmountTableAdapter();
        DataTable dtRefundAmount = TotalRefundAmount.GetData();
        string RefundAmount = Convert.ToString(dtRefundAmount.Rows[0]["RefundAmount"]);
        txtTotalRefundAmount.Value = RefundAmount;
    }


    protected void btnExport_Click(object sender, EventArgs e)
    {
        string Where = "";
        string timeFrom = dateFrom.Value;
        string ebayUserId = ddlebayUserId.Value;
        string timeTo = dateTo.Value;
        string statusId = ddlStatusId.Value;
        if (!string.IsNullOrEmpty(ebayUserId.Trim()) && ebayUserId != "Select")
        {
            ebayUserId = WebUtility.InputText(ebayUserId, 80);
            if (Where.Length > 0)
                Where += " And";
            Where += " Refund.EbayUserId = '" + ebayUserId + "'";
        }
        if (!string.IsNullOrEmpty(timeFrom) && !string.IsNullOrEmpty(timeTo))
        {
            timeFrom = WebUtility.InputText(timeFrom, 12);
            timeTo = WebUtility.InputText(timeTo, 12);
            DateTime TimeFrom = DateTime.Parse(timeFrom);
            DateTime TimeTo = DateTime.Parse(timeTo);
            if (Where.Length > 0)
                Where += " And";
            Where += " CONVERT (Varchar(8), Refund.RefundDate, 112) >= '" + TimeFrom.ToString("yyyyMMdd") + "' AND CONVERT (Varchar(8), Refund.RefundDate, 112) <= '" + TimeTo.ToString("yyyyMMdd") + "'";
        }
        if (!string.IsNullOrEmpty(statusId.Trim()))
        {
            statusId = WebUtility.InputText(statusId, 10);
            if (Where.Length > 0)
                Where += " And";
            Where += " Refund.StatusId = '" + statusId + "'";
        }

        string cmdText = @"Select   Refund.EbayUserId,Refund.OrderId,Refund.RefundId,Refund.TransactionId,Refund.BuyerId,Refund.BuyerFullName,Refund.AuditFlag,
            Refund.RefundAmount,Refund.StatusId,Refund.DisputeType,Refund.RefundDate,Refund.InvalidDate,Refund.Currency,o.OrderSalesRecordNumber,RefundType.RefundType as TypeName,c.FName as DepartmentName
            FROM Refund left join RefundType on Refund.RefundType=RefundType.Id
                left outer join (select OrderSalesRecordNumber,EbayUserId, OrderId from Orders group by OrderSalesRecordNumber,EbayUserId, OrderId) o on Refund.OrderId = o.OrderId And Refund.EbayUserId = o.EbayUserId
                left outer join [AIS2011120420456].dbo.t_item c on c.FItemClassId = 2 And c.FLevel = 1 and c.FitemId = Refund.BlameDepartments WHERE" + Where + " ORDER BY Refund.OrderId desc";

        DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText, null);
        ds.Tables[0].TableName = "RefundOrder";
        DataSetToExcel.DataSetOutExcel(ds.Tables[0], "退款订单");
    }
}
